package com.hospital.repository;

import com.hospital.model.Appointment;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.hospital.model.Appointment;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

@Repository
public class AppointmentRepository {
    private List<Appointment> appointments = new ArrayList<>();
    private Long nextId = 1L;

    public Appointment save(Appointment appointment) {
        if (appointment.getId() == null) {
            appointment.setId(nextId++);
        }
        appointments.add(appointment);
        return appointment;
    }

    public Optional<Appointment> findById(Long id) {
        return appointments.stream().filter(a -> a.getId().equals(id)).findFirst();
    }

    public List<Appointment> findAll() {
        return new ArrayList<>(appointments);
    }

    public List<Appointment> findByDoctorId(Long doctorId) {
        return appointments.stream()
                .filter(a -> a.getDoctor().getId().equals(doctorId))
                .collect(Collectors.toList());  // 修复后的代码
    }
}